﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ModemGSM;

namespace PhoneManager
{
    public partial class UCReport : UserControl
    {
        public delegate void UCReportSSelectedEventHandler(object sender, EventArgs e);
        public event UCReportSSelectedEventHandler EventUCReportSeleted;

        public delegate void UCReportCheckedEventHandler(object sender, EventArgs e);
        public event UCReportCheckedEventHandler EventUCReportChecked;

        private bool NotLoad;

        public UCReport()
        {
            InitializeComponent();
            TypeReport = TypeStatusReportEnum.SendConfirmation;
            TypeCode = "";
            Storage = "";
            Seleted = false;
            Checked = false;
            IndexStorage = -1;
            TypeCode = "";
            LblNumber.Text = "";
            LblMsg.Text = "";
            LblDtHrSent.Text = "";
            LblTzSent.Text = "";
            LblDtHrRec.Text = "";
            LblTzRec.Text = "";
            LblStatus.Text = "";
            NotLoad = true;
            ReadOnly = false;
        }

        public bool Checked
        {
            get { return ChkDel.Checked; }
            set
            {
                if (ReadOnly) return;
                ChkDel.Checked = value;
            }
        }

        public TypeStatusReportEnum TypeReport { get; set; }

        public string TypeCode { get; set; }

        public string Storage { get; set; }

        public int IndexStorage { get; set; }

        public int Status { get; set; }

        private bool _ReadOnly = false;
        public bool ReadOnly 
        {
            get { return _ReadOnly; } 
            set 
            {
                _ReadOnly = value;
                if (value)
                {
                    Seleted = false;
                    this.BackColor = SystemColors.Control;
                    this.BorderStyle = BorderStyle.None;
                    ChkDel.Checked = false;
                    ChkDel.Enabled = false;
                }
                else
                {
                    ChkDel.Enabled = true;
                }
            } 
        }

        public long TimeTicked { get; set; }

        private DateTime? _DateTimeSent = null;
        public DateTime? DateTimeSent
        {
            get { return _DateTimeSent; }
            set
            {
                _DateTimeSent = value;
            }
        }
        private DateTime? _DateTimeRec = null;
        public DateTime? DateTimeRec
        {
            get { return _DateTimeRec; }
            set
            {
                _DateTimeRec = value;
            }
        }

        public string Number
        {
            get { return LblNumber.Text; }
            set { LblNumber.Text = value; }
        }

        public string Message
        {
            get { return LblMsg.Text; }
            set { LblMsg.Text = value; }
        }

        public string StatusMessage
        {
            get { return LblStatus.Text; }
            set { LblStatus.Text = value; }
        }

        private TimeSpan? _TimezoneSent = null;
        public TimeSpan? TimezoneSent
        {
            get { return _TimezoneSent; }
            set
            {
                _TimezoneSent = value;
            }
        }

        private TimeSpan? _TimezoneRec = null;
        public TimeSpan? TimezoneRec
        {
            get { return _TimezoneRec; }
            set
            {
                _TimezoneRec = value;
            }
        }

        public GroupReportEnum GroupReport { get; set; }

        private bool _Seleted = false;

        public bool Seleted
        {
            get { return _Seleted; }
            set
            {
                if (ReadOnly) return;
                _Seleted = value;
                if (value)
                {
                    this.BackColor = Color.Honeydew;
                    this.BorderStyle = BorderStyle.FixedSingle;
                }
                else
                {
                    this.BackColor =  SystemColors.Control ;
                    this.BorderStyle = BorderStyle.None;
                }
                if (EventUCReportSeleted != null && !NotLoad)
                {
                    EventUCReportSeleted(this, new EventArgs());
                }
            }
        }

        private void UCReport_Leave(object sender, EventArgs e)
        {
            if (ReadOnly) return;
            this.Seleted = false;
        }

        private void UCReport_Click(object sender, EventArgs e)
        {
            if (ReadOnly) return;
            this.Seleted = true;
            this.Focus();
        }

        private void LblNumber_Click(object sender, EventArgs e)
        {
            if (ReadOnly) return;
            this.Seleted = true;
            this.Focus();
        }

        private void LblDtHrSent_Click(object sender, EventArgs e)
        {
            if (ReadOnly) return;
            this.Seleted = true;
            this.Focus();
        }

        private void LblDtHrRec_Click(object sender, EventArgs e)
        {
            if (ReadOnly) return;
            this.Seleted = true;
            this.Focus();
        }

        private void LblMsg_Click(object sender, EventArgs e)
        {
            if (ReadOnly) return;
            this.Seleted = true;
            this.Focus();
        }

        private void UCReport_Enter(object sender, EventArgs e)
        {
            if (ReadOnly) return;
            this.Seleted = true;
            this.Focus();
        }

        private void UCReport_Load(object sender, EventArgs e)
        {
            NotLoad = false;
            if (!_DateTimeSent.HasValue)
            {
                LblDtHrSent.Text = "";
                LblDtHrSent.Visible = false;
            }
            else
            {
                DateTime Dt = _DateTimeSent.Value;
                if (_TimezoneSent.HasValue)
                {
                    TimeSpan ts = System.TimeZone.CurrentTimeZone.GetUtcOffset(System.DateTime.Now);
                    ts = ts.Subtract(_TimezoneSent.Value);
                    Dt.Add(ts);
                }
                string Fmt = Properties.Settings.Default.DefaultDateFmt;
                Fmt = Fmt.Replace("DD", "dd").Replace("YYYY", "yyyy");
                LblDtHrSent.Text = Dt.ToString(Fmt + " HH:mm:ss");
            }
            if (!_TimezoneSent.HasValue || _TimezoneSent.Value.TotalSeconds == 0)
            {
                LblTzSent.Text = "";
                LblTzSent.Visible = false;
            }
            else
            {
                TimeSpan ts = System.TimeZone.CurrentTimeZone.GetUtcOffset(System.DateTime.Now);
                ts = ts.Subtract(_TimezoneSent.Value);
                if (ts.TotalMinutes < 0)
                {
                    LblTzSent.Text = "-" + ts.Hours.ToString().PadLeft(2, '0') + ":" + ts.ToString().PadLeft(2, '0');
                }
                else
                {
                    if (ts.TotalSeconds != 0)
                    {
                        LblTzSent.Text = ts.Hours.ToString().PadLeft(2, '0') + ":" + ts.Minutes.ToString().PadLeft(2, '0');
                    }
                    else
                    {
                        LblTzSent.Text = "";
                        LblTzSent.Visible = false;
                    }
                }
            }
            if (!_DateTimeRec.HasValue)
            {
                LblDtHrRec.Text = "";
                LblDtHrRec.Visible = false;
            }
            else
            {
                DateTime Dt = _DateTimeRec.Value;
                if (_TimezoneRec.HasValue)
                {
                    TimeSpan ts = System.TimeZone.CurrentTimeZone.GetUtcOffset(System.DateTime.Now);
                    ts = ts.Subtract(_TimezoneRec.Value);
                    Dt.Add(ts);
                }
                string Fmt = Properties.Settings.Default.DefaultDateFmt;
                Fmt = Fmt.Replace("DD", "dd").Replace("YYYY", "yyyy");
                LblDtHrRec.Text = Dt.ToString(Fmt + " HH:mm:ss");
            }
            if (!_TimezoneRec.HasValue || _TimezoneRec.Value.TotalSeconds == 0)
            {
                LblTzRec.Text = "";
                LblTzRec.Visible = false;
            }
            else
            {
                TimeSpan ts = System.TimeZone.CurrentTimeZone.GetUtcOffset(System.DateTime.Now);
                ts = ts.Subtract(_TimezoneRec.Value);
                if (ts.TotalMinutes < 0)
                {
                    LblTzRec.Text = "-" + ts.Hours.ToString().PadLeft(2, '0') + ":" + ts.ToString().PadLeft(2, '0');
                }
                else
                {
                    if (ts.TotalSeconds != 0)
                    {
                        LblTzRec.Text = ts.Hours.ToString().PadLeft(2, '0') + ":" + ts.Minutes.ToString().PadLeft(2, '0');
                    }
                    else
                    {
                        LblTzRec.Text = "";
                        LblTzRec.Visible = false;
                    }
                }
            }
            if (LblNumber.Text == "")
            {
                LblNumber.Visible = false;
            }
            if (GroupReport == GroupReportEnum.ReceiverOk)
            {
                if (Status < 0)
                {
                    LblStatus.ForeColor = Color.Red;
                }
                else
                {
                    LblStatus.ForeColor = Color.Green;
                }
            }
            else
            {
                LblStatus.ForeColor = Color.Red;
            }
        }

        private void ChkDel_CheckedChanged(object sender, EventArgs e)
        {
            if (ReadOnly) return;
            if (EventUCReportChecked != null)
            {
                EventUCReportChecked(this, new EventArgs());
            }
        }

        private void flowLayoutPanelSent_Click(object sender, EventArgs e)
        {
            if (ReadOnly) return;
            this.Seleted = true;
            this.Focus();
        }

        private void flowLayoutPanelRec_Click(object sender, EventArgs e)
        {
            if (ReadOnly) return;
            this.Seleted = true;
            this.Focus();
        }
        
        private void LblTzSent_Click(object sender, EventArgs e)
        {
            if (ReadOnly) return;
            this.Seleted = true;
            this.Focus();
        }

        private void LblTzRec_Click(object sender, EventArgs e)
        {
            if (ReadOnly) return;
            this.Seleted = true;
            this.Focus();
        }

        private void lblTitRec_Click(object sender, EventArgs e)
        {
            if (ReadOnly) return;
            this.Seleted = true;
            this.Focus();
        }

        private void LblTitSent_Click(object sender, EventArgs e)
        {
            if (ReadOnly) return;
            this.Seleted = true;
            this.Focus();
        }

        private void LblStatus_Click(object sender, EventArgs e)
        {
            if (ReadOnly) return;
            this.Seleted = true;
            this.Focus();
        }

    }
}
